package net.tolkv.tdd;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class BookStoreService extends Store {
	private HashMap<String, List<Book>> authors;
	private HashMap<String, List<Book>> titles;
	private Set<Book> books;

	public BookStoreService() {
		super();
		authors=new HashMap<String,List<Book>>();
		titles=new HashMap<String,List<Book>>();
		books= new HashSet<Book>();
	}

	public int getTotalBooksAmount() {
		return books.size();
	}

	public void addBook(Book book) {
		if(book==null)
			throw new NullPointerException("Add book is null");
		List<Book> tmp;
		if(!authors.containsKey(book.getAuthor())){
			tmp=new ArrayList<Book>(2);
			tmp.add(book);
			authors.put(book.getAuthor(), tmp);
		} else {
			authors.get(book.getAuthor()).add(book);
		}
		
		if(!titles.containsKey(book.getTitle())){
			tmp=new ArrayList<Book>(2);
			tmp.add(book);
			titles.put(book.getTitle(), tmp);
		} else {
			titles.get(book.getTitle()).add(book);
		}
		
		books.add(book);
	}

	public Set<String> getAuthors() {
		return authors.keySet();
	}

	public Set<String> getTitles() {
		
		return titles.keySet();
	}

	public List<Book> findByAuthor(String string) {
		return authors.get(string);
	}

	public List<Book> findByTitle(String string) {
		return titles.get(string);
	}
}
